package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dto.SetmealDto;
import com.example.entity.Setmeal;
import com.example.dao.SetmealDao;
import com.example.entity.SetmealDish;
import com.example.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 套餐(Setmeal)表服务实现类
 *
 * @author makejava
 * @since 2022-05-23 14:39:52
 */
@Service("setmealService")
public class SetmealServiceImpl extends ServiceImpl<SetmealDao,Setmeal> implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private SetmealDishServiceImpl setmealDishService;
    @Autowired
    private  SetmealServiceImpl setmealService;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Setmeal queryById(Long id) {
        return this.setmealDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Setmeal> queryAllByLimit(int offset, int limit) {
        return this.setmealDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param setmeal 实例对象
     * @return 实例对象
     */
    @Override
    public Setmeal insert(Setmeal setmeal) {
        this.setmealDao.insert(setmeal);
        return setmeal;
    }

    /**
     * 修改数据
     *
     * @param setmeal 实例对象
     * @return 实例对象
     */
    @Override
    public Setmeal update(Setmeal setmeal) {
        this.setmealDao.update(setmeal);
        return this.queryById(setmeal.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.setmealDao.deleteById(id) > 0;
    }

    @Override
    @Transactional
    public int saveSetmeal(SetmealDto setmealDto) {
        setmealDto.setIsDeleted(0);
        boolean a=this.save(setmealDto);
        List<SetmealDish>list=setmealDto.getSetmealDishes();
        for (int i = 0; i <list.size() ; i++) {
            list.get(i).setSetmealId(String.valueOf(setmealDto.getId()));
            list.get(i).setSort(0);
            list.get(i).setIsDeleted(0);
        }
        boolean b=setmealDishService.saveBatch(list);
        if (a&&b){
            return 0;
        }else
        {
            return 1;
        }

    }

    @Override
    @Transactional
    public int deleteSetmeal(Long id) {
        LambdaQueryWrapper<SetmealDish>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,String.valueOf(id));
        boolean b=setmealDishService.remove(queryWrapper);
        if(b){
            return 0;
        }else{
            return 1;
        }

    }
}